package cn.yanglq.Rss.core.rssManage;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by yanglq on 2017/6/5.
 */
public class Rss {
    public static String RSS_DOM_ROOT_TITLE = "//channel/title";

    public static String RSS_DOM_ROOT_LINK = "//channel/link";

    public static String RSS_DOM_ROOT_DESCRIPTION = "//channel/description";

    public static String RSS_DOM_CHILRDEN_ROOT = "//channel/item";

    public static String RSS_DOM_CHILRDEN_ROOT_TITLE = "title";

    public static String RSS_DOM_CHILRDEN_ROOT_LINK = "link";

    public static String RSS_DOM_CHILRDEN_ROOT_PUBDATE = "pubDate";

    public static String RSS_DOM_CHILRDEN_ROOT_DESCRIPTION = "description";

    public static String DATABASES_PATH = "#rss_databases.mdb";
    private Document document;

    public Document parse(URL url) throws DocumentException {
        SAXReader reader = new SAXReader();
        document = reader.read(url);
        return document;
    }

    public void parseUrl(URL url) throws DocumentException {
        SAXReader reader = new SAXReader();

        document = reader.read(url);
        System.out.print(document.getDocument().getRootElement().getName());
// return document;
    }

    public List getXmlInfo(String path, URL url) {
        List info = new ArrayList();
        try {
            Document document = parse(url);
            info = document.selectNodes(path);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return info;
    }

    public List getXmlInfo(String path) throws DocumentException {
        List info = new ArrayList();
// Document document = parse(url);
        info = document.selectNodes(path);
        return info;
    }

    public Element getFirstNodeTitle(String path, URL url) {
        List list = getXmlInfo(path, url);
        Element element = (Element) list.get(0);
        return element;
    }

    public Element getFirstNodeTitle(String path) throws DocumentException {
        List list = getXmlInfo(path);
        Element element = (Element) list.get(0);
        return element;
    }

    /**
     * 通过rss地址获取rss内容
     *
     * @param uri
     * @return
     */
    public RssBean getRssBean(String uri) {

        RssBean rss = new RssBean();

// Test xu = new Test();
        URL url;
        try {
            url = new URL(uri);

            parseUrl(url);

            List listRoot = getXmlInfo("//channel");
            for (Iterator iter = listRoot.iterator(); iter.hasNext(); ) {
                Element element = (Element) iter.next();

                Node title = element.selectSingleNode(RSS_DOM_ROOT_TITLE);
                Node link = element.selectSingleNode(RSS_DOM_ROOT_LINK);
                Node description = element
                        .selectSingleNode(RSS_DOM_ROOT_DESCRIPTION);
                System.out.println(description.getText()
                        + description.getText());
                rss.setTitle(title.getText());
                rss.setDescription(description.getText());
                rss.setLink(link.getText());

            }
            System.out.println("----------------------");
/**
 * 获取内容项
 */
            List list = getXmlInfo(RSS_DOM_CHILRDEN_ROOT);
            for (Iterator iter = list.iterator(); iter.hasNext(); ) {
                Element element = (Element) iter.next();
                Node title = element
                        .selectSingleNode(RSS_DOM_CHILRDEN_ROOT_TITLE);
                Node link = element
                        .selectSingleNode(RSS_DOM_CHILRDEN_ROOT_LINK);
                Node time = element
                        .selectSingleNode(RSS_DOM_CHILRDEN_ROOT_PUBDATE);

                Node description = element
                        .selectSingleNode(RSS_DOM_CHILRDEN_ROOT_DESCRIPTION);
/*
* System.out.println("标题: " + title.getText());
* System.out.println("时间: " + time.getText());
* System.out.println("地址: " + link.getText());
* System.out.println("描述  : " + description.getText());
* System.out
* .println("======================================================"
* );
*/
                RssBean rs = new RssBean();
                rs.setTitle(title.getText());
                rs.setDate(time.getText());
                rs.setDescription(description.getText());
                rs.setLink(link.getText());
                rss.getItems().add(rs);

            }
            return rss;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public RssBean getRSSrsult(String url) throws IOException {

// String url = "http://sun-store.blogbus.com/";
        RssUtil r = new RssUtil();
// Test t = new Test();
        RssBean rs;

/**
 * 判断是否为html，如果是html并查找出rss地址
 */
        String result = r.isHtmlOrXml(url);

        if ("xml".equals(result)) {

            rs = getRssBean(url);
            System.out.print(rs.getTitle());

        } else if ("eorr".equals(result)) {
            rs = null;
            System.out.print("不支持rss");
        } else {
            rs = getRssBean(result);
            System.out.print(rs.getTitle());

        }
        return rs;
    }

//    public static void main(String args[]) throws IOException {
//
//        String url = "https://www.zhihu.com/rss";
//        Rss t = new Rss();
//        RssBean rs;
//        rs = t.getRSSrsult(url);
//        System.out.println(rs.getTitle());
//
//    }

    public void setDocument(Document document) {
        this.document = document;
    }

    public Document getDocument() {
        return document;
    }
}
